package com.xjl.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xjl.demo.Admin;
import com.xjl.service.AdminService;
import com.xjl.service.impl.AdminServiceImpl;
@WebServlet("/Chage.do")
/**
 * 修改管理员密码
 * 根据ID,PWD，修改新的密码
 * ID从admin中获取
 * @author JianLin_Xu
 *
 */
public class ChangeAdminPwdServlet extends HttpServlet {
	protected void service(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		//设置请求编码
		req.setCharacterEncoding("utf-8");
		//设置响应编码
		resp.setContentType("text/html;utf-8");
		//处理请求信息
		HttpSession session = req.getSession();//重定向需要用到session传数据
		Admin admin = (Admin)session.getAttribute("admin");
		int adminid = admin.getAdminid();
		String adminpwd = req.getParameter("adminpwd");
		String newpwd = req.getParameter("newpwd");
		AdminService as = new AdminServiceImpl();
		int index = as.ChangeAdminPwdService(adminid, adminpwd, newpwd);
		if(index==1){
			session.setAttribute("flag1", "修改成功，下次登录记得用新密码哦！");
		}else if(index==-1){
			session.setAttribute("flag1", "原始密码错误，请重新输入！");
		}else{
			session.setAttribute("flag1", "修改失败，请稍候重试！");
		}
		resp.sendRedirect("/pcshop/admin/pass.jsp");//用重定向反正数据重复提交，造成数据库混乱
		
		
	}
}
